import QtQuick
import QtQuick.Controls

Rectangle {
  id: topWindow
  visible: true
  color: "red"

  // 串口选择
  ComSelect {
    id: com_select
    anchors.top: topWindow.top
    anchors.right: comboBox_baudRate.left
    width: topWindow.width / 5
    height: topWindow.height / 3
    Component.onCompleted: {
      FindCom.getComInfoList();
    }
  }

  // 波特率的选择
  ComboBox {
    id: comboBox_baudRate
    anchors.top: topWindow.top
    anchors.right: topWindow.right
    width: com_select.width
    height: com_select.height
    model: [9600, 115200, 57600, 230400]
    currentIndex: 0
  }

  signal backSelectComMes(string com_name, int index, bool is_connect, int baudRate)
  signal closeSelectCom
  // 连接按钮的定义
  Button {
    anchors.bottom: topWindow.bottom
    anchors.top: comboBox_baudRate.bottom
    anchors.right: topWindow.right
    width: 100
    height: 40
    text: qsTr("连接")

    // 添加属性来接收外部传入的值
    property string selectedComPort: com_select.currentText
    property int selectedComIndex: comboBox_baudRate.currentIndex
    property string selectedBaudRate: comboBox_baudRate.currentText

    onClicked: {
      var is_connect = false;
      if (text === qsTr("连接")) {
        text = qsTr("断开连接");
        is_connect = true;
        console.log("开始连接 ", selectedComPort, " 串口");
        backSelectComMes(selectedComPort, selectedComIndex, is_connect, selectedBaudRate);
      } else {
        text = qsTr("连接");
        is_connect = false;
        console.log("开始断开 ", selectedComPort, " 串口");
        closeSelectCom();
      }
    }
  }
}
